package com.yc.class_attendance;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.StrictMode;

import com.squareup.leakcanary.RefWatcher;
import com.yc.class_attendance.entity.DaoMaster;
import com.yc.class_attendance.entity.DaoSession;
import com.yc.class_attendance.utils.CrashHandler;
import com.yc.class_attendance.utils.RealmHelper;
import com.yc.class_attendance.utils.SharePreferenceUtil;
import com.yc.class_attendance.utils.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by PC on 2017/4/13.
 */

public class MyApp extends Application {
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    private static MyApp sInstance;
    public SharePreferenceUtil sharePreferenceUtil;
    public List<String> bigenCard = new ArrayList<>();
    public List<String> endCard = new ArrayList<>();
    public List<String> meetingId = new ArrayList<>();
    public List<String> courseId = new ArrayList<>();
    public List<String> class_attemdamce = new ArrayList<>();

    private int giDestPort;
    private String Card;
    private RefWatcher refWatcher;

    private CrashHandler crashHandler;
    public MyApp() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        RealmConfiguration configuration=new RealmConfiguration.Builder()
                .name(RealmHelper.DB_NAME)
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(configuration);

        setDatabase();
        sInstance = this;
        sharePreferenceUtil = new SharePreferenceUtil(this, "class");
        if (BuildConfig.DEBUG) {
            Logger.init();
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build());
            StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder()
                    .detectAll()
                    .penaltyLog();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                threadPolicyBuilder.penaltyDeathOnNetwork();
            }
            StrictMode.setThreadPolicy(threadPolicyBuilder.build());

        }
        //发布版本 启动异常重启模式 发布时：打开，测试时：关闭 因为这可以屏蔽 Bug 都交给系统异常处理
        crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());





//                if(LeakCanary.isInAnalyzerProcess(this)){
//            return;
//        }
//        refWatcher = LeakCanary.install(this);
    }
//    public static RefWatcher getRefWatcher(Context context) {
//        MyApp  application= (MyApp) context.getApplicationContext();
//        return application.refWatcher;
//    }
    /**
     * 设置greenDao
     */
    private void setDatabase() {
        // 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 可能你已经注意到了，你并不需要去编写「CREATE TABLE」这样的 SQL 语句，因为 greenDAO 已经帮你做了。
        // 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
        // 所以，在正式的项目中，你还应该做一层封装，来实现数据库的安全升级。
        mHelper = new DaoMaster.DevOpenHelper(this, "record.db", null);
        db = mHelper.getWritableDatabase();

        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public static MyApp getsInstance() {
        return sInstance;
    }

    public int getgiDestPort() {
        return this.giDestPort;
    }

    public void setgiDestPort(int c) {
        this.giDestPort = c;
    }

    public void setCard(String Card) {
        this.Card = Card;
    }

    public String getCard() {
        return Card;
    }

    public List<String> getMeetingId() {
        return meetingId;
    }

    public void setMeetingId(List<String> meetingId) {
        this.meetingId = meetingId;
    }

    public List<String> getBigenCard() {
        return bigenCard;
    }

    public void setBigenCard(List<String> bigenCard) {

        this.bigenCard = bigenCard;
    }

    public List<String> getEndCard() {
        return endCard;
    }

    public void setEndCard(List<String> endCard) {
        this.endCard = endCard;

    }

    public List<String> getCourseId() {
        return courseId;
    }

    public void setCourseId(List<String> courseId) {
        this.courseId = courseId;

    }

    public List<String> getClass_attemdamce() {
        return class_attemdamce;
    }

    public void setClass_attemdamce(List<String> class_attemdamce) {
        this.class_attemdamce = class_attemdamce;

    }

}
